[LegacyColorValue = true]; 

{ TSM Volume Dist:  Create volume distributions
  Copyright 1995-1999, P.J. Kaufman.  All rights reserved.  }

  inputs:  enddate(940331), mtype(0), type(1), nint(20), max(0), nsd(.5),
               period(40), lead(0);
  arrays:  hist[50](0);
  vars:     vhigh(0), vlow(0), size(0), loc(0), pchg(0), ix(0), avgchg(0),
               sdchg(0), sdpchg(0), avgv(0), sdvol(0), sdv(0);

  { type = 1, Histrogram
                   Max = 0, use rolling period frequency
                   Max > 0, use as maximum volume
    type = 2, Create scatter diagram of volume vs % price change
                   nsd = small % price changes to exclude when writing file
                   period <> 0 use moving stdev of price changes
  }
  { Create a histogram of n intervals "nint" }
    if type = 1 then begin
       if max = 0 then begin
             vhigh = highest(volume[1],period);
             vlow = lowest(volume[1],period);
             end
          else begin
             vhigh = max;
             vlow = 0;
             end;
  { Find interval size, put excess in intervals+1 }
          if volume = 0 then hist[0] = hist[0] + 1
             else begin
                 size = (vhigh - vlow) / nint;
                 loc = (volume - vlow) / size;
                 if loc < 1 then loc = 1
                    else if loc > nint then loc = nint + 1;
                 hist[loc] = hist[loc] + 1;
                 end;
          if date >= enddate then begin
             size = (vhigh - vlow) / nint;
             for ix = 0 to nint+1 begin
                  loc = vlow + ix*size;
                  print (date:6:0, ix:3:0, hist[ix]:4:0, loc:10:0);
                  print (File("C:\TSM5\voldist.txt"),date:6:0,",",
                            ix:3:0, ",",hist[ix]:4:0, ",",loc:10:0);
                  end;
             end;
          end;

    if type = 2 then begin
       pchg = (close - close[1])*100 / close;
       if period = 0 then begin
               print (pchg:4:2,volume:8:0);
               print (File("C:\TSM5\voldist.txt"),pchg[lead]:4:2,",",volume:10:0);
               end
          else begin
               avgchg = average(pchg[1],period);
               sdchg = stddev(pchg[1],period);
               sdpchg = 0;
               if sdchg <> 0 then sdpchg = (pchg - avgchg) / sdchg;
               avgv = average(volume[1],period);
               sdvol = stddev(volume[1],period);
               sdv = 0;
               if sdvol <> 0 then sdv = (volume - avgv) / sdvol;
               print (sdpchg:5:2,sdv[lead]:5:2);
               print (File("C:\TSM5\voldist.txt"),sdpchg:5:2,",",sdv[lead]:5:2);
           end;
        end;